//
//  LYMainViewController.swift
//  微博
//
//  Created by liuerying on 16/5/19.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit

class LYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()


    addChildViewControllers()
//        print(tabBar.classForCoder)
        
        let mainTabbar = LYMainTabbarView()
        setValue(mainTabbar, forKey: "tabBar")
        
        mainTabbar.plusBtn.addTarget(self, action: #selector(LYMainViewController.pulsBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
//        print(tabBar.classForCoder)
    }

 @objc private func pulsBtnClick(){
    
        print("点击了")
    
    let comoseView = LYComposeView()
    
    comoseView.show(targetVC: self)
    
//    let widonw = UIApplication.sharedApplication().keyWindow
//    
//    widonw?.addSubview(comoseView)
//    
    
    
    }
    
    //添加控制器
  private func addChildViewControllers(){
    
    addChildViewController(LYHomeTableViewController(), title: "首页", imageName: "tabbar_home",index: 0)
    addChildViewController(LYMessageTableViewController(), title: "消息", imageName: "tabbar_message_center",index: 1)
    addChildViewController(LYDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",index: 2)
    addChildViewController(LYProfileTableViewController(), title: "我的", imageName: "tabbar_profile",index: 3)
    }
    
    //封装的添加tabar自控制器的方法
    private  func addChildViewController(VC: UIViewController,title : String,imageName : String,index:Int) {
    
    //设置导航栏和标签栏的的字体内容 统一一样的内容
        
        let tabbaritem = LYTabbarItem()
        VC.tabBarItem = tabbaritem
        
        VC.title = title
        //给底部按钮添加tag值
    VC.tabBarItem.tag = index
//    VC.tabBarItem.title = title
    //创建图片
        VC.tabBarItem.image = UIImage(named: imageName)
  
    //设置选中图片为原始图片的颜色
        VC.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    //设置单个选中字体的颜色
    VC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)

    //设置标签栏统一的字体颜色
//    tabBar.tintColor=UIColor.orangeColor()
    
    VC.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
    
    
        let nav = LYBaseNavController(rootViewController: VC)
        
        addChildViewController(nav)
    }
    
    //实现协议方法
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print(item.tag)
        var index = 0
        //遍历tabbar的子视图
        for v in tabBar.subviews{
        
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                //确定点击的是哪一个tabbarButton
                if index == item.tag {
                
                    for subView in v.subviews{
                
                        if subView.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){
                        
                            subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            
                            UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 4, options: [], animations: { 
                                
                                //在动画闭包中 恢复到初始值
                                subView.transform = CGAffineTransformIdentity
                                
                                }, completion: { (_) in
//                                    print("ok")
                            })
                            
                        }
                        
                    }
                    
                }
                index += 1
                
            }
        }
  
    }
 
    
}
